﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FrienDev.BusinessRules;
using FrienDev.Data;

public partial class Controls_Activity_FeedBackSummary : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string activityIdString = Request["activityId"];
        if (!int.TryParse(activityIdString, out m_activityId))
            Response.Redirect("~/Home/Default.aspx");
        if (!ActivityRules.Instance.IsCurrentUserIsActivityPromoter(m_activityId))
            Response.Redirect("~/Home/Default.aspx");

        IEnumerable<User> users = ActivityRules.Instance.GetActivityJoined(m_activityId, -1);
        List<string> userIds = new List<string>();
        foreach (User user in users)
        {
            userIds.Add(user.UserId);
        }
        List<int[]> marks = ActivityRules.Instance.GetUsersFeedBackMark(m_activityId, userIds);
        int markCount = marks.Count != 0 ? marks.Count : 1;
        int overallSum = 0;
        int contentSum = 0;
        int organizeSum = 0;
        foreach (int[] mark in marks)
        {
            overallSum += mark[0];
            contentSum += mark[1];
            organizeSum += mark[2];
        }
        lblOverallMark.Text = overallSum.ToString();
        lblContentMark.Text = contentSum.ToString();
        lblOrganizeMark.Text = organizeSum.ToString();
        lblOverallAverage.Text = (overallSum / markCount).ToString();
        lblContentAverage.Text = (contentSum / markCount).ToString();
        lblOrganizeAverage.Text = (organizeSum / markCount).ToString();

        //自定义问题数据绑定
        List<string> questions = ActivityRules.Instance.GetCustomFeedBackQuestions(m_activityId);
        Dictionary<string, List<string>> answers = ActivityRules.Instance.GetUsersCustomFeedBackAnswer(m_activityId, userIds);
        questionAnswers = new Dictionary<string, List<CustomQuestionAnswers>>();
        if (questions == null || answers == null)
            return;
        for (int i = 0; i < questions.Count; i++)
        {
            count = -1;
            List<CustomQuestionAnswers> tList = new List<CustomQuestionAnswers>();
            foreach (KeyValuePair<string, List<string>> userAnswers in answers)
            {
                if (userIds.Count >= count)
                    count++;
                if (userAnswers.Value.Count - 1 != questions.Count)
                    continue;
                CustomQuestionAnswers cqa = new CustomQuestionAnswers();
                cqa.Name = userAnswers.Key + "：";
                cqa.Question = questions[i];
                cqa.Answer = userAnswers.Value[i];
                cqa.UserHomeUrl = ApiHelper.Instance.GetUserHomepageLink(userIds[count]);
                tList.Add(cqa);
            }
            questionAnswers.Add(questions[i], tList);
        }
        GridView1.DataSource = questionAnswers;
        GridView1.DataBind();
    }

    private int count = 0;
    private int m_activityId = -1;
    private Dictionary<string, List<CustomQuestionAnswers>> questionAnswers;
}

class CustomQuestionAnswers
{
    public string UserHomeUrl { get; set; }
    public string Name { get; set; }
    public string Answer { get; set; }
    public string Question { get; set; }
}